import datetime
import json
import random

from flask import Blueprint
from RootPath import RootPath
import os

from common.YmlUtil import YmlUtil
from common.contactStr import sqlContact, mqttMessageContact
from common.mqttUtil import on_mqtt_connect, publishMqttData
from common.mysqlUtil import exc_sql
from dataGenerator.shenshui.tankLevel import tanklevelValue, openValveState
from dataGenerator.shenshui.contenPara import *
generPr = Blueprint("deviceGenerator", __name__)
path = os.path.join(RootPath.getRootPath(), "Application.yml")
MQTTconf = YmlUtil().readYml(path,"mqtt_test_SS")
MySQLconf = YmlUtil().readYml(path,"mysql_test_243")
deviceID="9"
h = int((datetime.datetime.now()).strftime("%H"))
m = int((datetime.datetime.now()).strftime("%M"))

class SSdeviceGenerator():
    def exc(self):
        peakValues= tanklevelValue()
        tanklevel= peakValues.get("tanklevel")
        ValveState= peakValues.get("valveState")
        print(" =====SS=======")
        pumpstat1="0"
        pumpstat2="0"
        pumpstat3="0"
        pumpstat4="0"
        if h>=7 and h<=9:
            pumpstat1="1"
            pumpstat2="1"
        elif h>=11 and h<=13:
            pumpstat2="1"
            pumpstat3="1"
        elif h>=18 and h<=19:
            pumpstat3="1"
            pumpstat4="1"
        else:
            pumpstat1="1"

        point_value={"TankHHLevel":"4","TankHighLevel":"3.5",
                     "TankLevel":str(tanklevel),"TankLLLevel":"0.5",
                     "TankLowLevel":"1","TankMidLevel":"2",
                     "ValveState":str(ValveState),"InstantaneousFlow":str(InstantaneousFlow),
                     "No1InletEVOpen":str(ValveState),"SP97":json.dumps((random.randint(10,30)/10)),
                     "No1FrequencyOutput":json.dumps(random.randint(30,40)),"No2FrequencyOutput":json.dumps(random.randint(30,40)),"No3FrequencyOutput":json.dumps(random.randint(30,40)),
                     "No4FrequencyOutput":json.dumps(random.randint(30,40)),
                     "OutletPressure":str(OutletPressure),"InletPressure":str(InletPressure),
                     "GivenPressure":"40","PositiveActivePower":"0","InletRange":"1",
                     "OutletRange":"1","No1FrequencyPower":json.dumps(random.randint(30,40)),"No2FrequencyPower":json.dumps(random.randint(30,40)),
                     "No3FrequencyPower":json.dumps(random.randint(30,40)),"No4FrequencyPower":json.dumps(random.randint(30,40)),"No1FrequencyOper":pumpstat1,
                     "No2FrequencyOper":pumpstat2,"No3PowerFrequencyOper":pumpstat3,"No4PowerFrequencyOper":pumpstat4,
                     "SleepingFrequency":"800","ReducedFrequency":"0"}

        energy_value ={"active_power":json.dumps(activePowerAcc()),"CumulativeFlow":json.dumps(CumulativeFlowAcc())}
        # print(energy_value)
        point_value.update(energy_value)
        sqldata=sqlContact(deviceID,point_value)
        mqttdata=json.dumps(mqttMessageContact(deviceID,point_value))
        # print(sqldata)
        # print(mqttdata)
        on_mqtt_connect(MQTTconf)
        publishMqttData(MQTTconf,mqttdata)#推送mqtt消息要用json dump方法
        exc_sql(sqldata,MySQLconf)




# SSdeviceGenerator().exc()


